﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.WebSocket.SubProtocol;
using SuperSocket.WebSocket;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

using XBJCYGC;
using System.IO;
using System.Data;
using System.IO.Compression;
using System.Web.Http;
using System.Reflection;

namespace SocketData
{
    public class API : JsonSubCommand<JObject>
    {
        protected override void ExecuteJsonCommand(WebSocketSession session, JObject commandInfo)
        { 
            try
            {
                //int t1 = System.Environment.TickCount;
                var requestpath = commandInfo["_requestpath"].ToString();//格式： control/action
                var ident_id = commandInfo["_ident_id"].ToString();
                var data = commandInfo["_data"].ToString();
                var path = requestpath.Split('/');
                //反向解析到control中去。
                var ws = ((MyWS)session.AppServer);
                var para1 = commandInfo["_data"];
                var result=ws.CtrlRunner.RunController(path[0] + "Controller",path[1], (JObject)para1);
                //返回结果
                session.Send(new
                {
                    _requestpath = requestpath,
                    _ident_id = ident_id,
                    _data = result
                }.toJson());
                 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                commandInfo.Add("_exception", ex.Message);
                session.Send(commandInfo.ToString(Formatting.None, new JsonConverter[1] { new ExpandoObjectConverter() }));
            }
        }
    }
}
